package com.example.content.fragment.me.userproduct

import android.os.Bundle
import android.widget.ImageView
import android.widget.TextView
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.example.content.*
import com.example.content.activity.PhotoDetailActivity
import com.example.content.adapter.recycle.BindListener
import com.example.content.adapter.recycle.RecyclerAdapter
import com.example.content.adapter.recycle.entity.ItemEntity
import com.example.content.ext.convertUrl
import com.example.content.manager.ActivityMgr
import com.example.content.manager.GlobalMgr
import com.example.content.repository.ApiService
import com.example.content.repository.bean.ProductCollectInfo

/**
 * Created by zhudongao
 * Desc
 * Date 2/6/21
 */
class CollectListFragment(pageType:Int) : BaseProductFragment<ProductCollectInfo>(pageType){

    override fun getAdapterFun(): RecyclerAdapter<ProductCollectInfo> = RecyclerAdapter(object :
        BindListener<ProductCollectInfo> {
        override fun convert(holder: BaseViewHolder, item: ItemEntity<ProductCollectInfo>) {
            val proBg = holder.getView<ImageView>(R.id.image)
            context?.let {
                Glide.with(it).load(item.data.cover?.convertUrl()).into(proBg)
            }
            val titleView = holder.getView<TextView>(R.id.product_title)
            titleView.text = item.data.description
            val visitorNum = holder.getView<TextView>(R.id.visitor_num)
            visitorNum.text = item.data.view_num.toString()
            val productType = holder.getView<ImageView>(R.id.product_type)
            if (item.data.resource_type == 1) {
                productType.setImageResource(R.drawable.ic_video)
            } else {
                productType.setImageResource(R.drawable.ic_photo)
            }
        }
    })

    override fun initListener() {
        super.initListener()
        adapter.setOnItemClickListener { adapter, view, position ->
//            val bundle = Bundle()
//            bundle.putInt(KEY_FRAGMENT, PRODUCT_DETAIL)
//            bundle.putInt(PRODUCT_ID, (adapter.getItem(position) as ItemEntity<ProductCollectInfo>).data.id)
//            ActivityMgr.startFragment(bundle)

            val bundle = Bundle()
            bundle.putInt(PRODUCT_ID, (adapter.getItem(position) as ItemEntity<ProductCollectInfo>).data.id)
            ActivityMgr.startActivity(PhotoDetailActivity::class.java, bundle)
        }

        viewModel.collectResult.observe(this, observer = {
            if (it.isEmpty()) {
            } else {
                adapter.data.clear()
                for (productInfo in it) {
                    val item = ItemEntity<ProductCollectInfo>(data = productInfo)
                    adapter.data.add(item)
                }
                adapter.notifyDataSetChanged()
            }
        })
        GlobalMgr.follow.observe(this, {
            queryData()
        })
    }

    companion object {
        @JvmStatic
        fun newInstance() = CollectListFragment(USER_COLLECT)
    }
}